package com.saxon.blog.controller.thymeleaf;

import com.saxon.blog.pojo.BlogType;
import com.saxon.blog.service.Blog.BlogTypeService;
import com.saxon.blog.service.Blog.BlogTypeServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

@Controller
public class adminController {
    BlogTypeService blogTypeService;

    @Autowired
    public void setBlogTypeService (BlogTypeService blogTypeService) {
        this.blogTypeService = blogTypeService;
    }

    //首页
    @GetMapping ("admin/main")
    public String main () {
        return "admin/main";
    }

    //登录
    @GetMapping ( {"tologin", "login"})
    public String tologin () {
        return "admin/login";
    }

    //未登录
    @GetMapping ("Unauthorized")
    public String Unauthorized () {
        return "admin/unAuthen";
    }

    //    对下面登录的路径映射
    @GetMapping ("admin/index.html")
    public String index () {
        return "admin/index";
    }

    @PostMapping ("login")
    public String login (String username, String password, Model model, boolean rememberMe) {
        Subject subject = SecurityUtils.getSubject ();

        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken (username, password);
        try {
            usernamePasswordToken.setRememberMe (rememberMe);
            subject.login (usernamePasswordToken);
            return "redirect:admin/index.html";
        } catch (UnknownAccountException e) {
            model.addAttribute ("msg", "用户错误");
            return "admin/login";
        } catch (IncorrectCredentialsException e) {
            model.addAttribute ("msg", "密码错误");
            return "admin/login";
        }
    }

    //注销
    @GetMapping ("logout")
    public String logout () {
        Subject subject = SecurityUtils.getSubject ();
        subject.logout ();
        return "admin/login";
    }

    @GetMapping ("admin/page/userList")
    public String userList () {
        return "admin/page/userList";
    }

    @GetMapping ("admin/page/BlogList")
    public String BlogList () {
        return "admin/page/BlogList";
    }

    @GetMapping ("admin/page/addBlog")
    public String addBlog (Model model) {
        List<BlogType> blogTypes = blogTypeService.QueryAllType ();
        model.addAttribute ("blogTypes",blogTypes);
        return "admin/page/addBlog";
    }

    @GetMapping ("admin/page/gallery")
    public String gallery () {
        return "admin/page/gallery";
    }

    @GetMapping("admin/page/blogType")
    public String blogType(){
        return "admin/page/blogType";
    }

    @GetMapping("/admin/page/fileUpload")
    public String fileUpload(){
        return "/admin/page/fileUpload";
    }
}
